package com.ruoyi.internet.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.internet.api.domain.IntUser;
import com.ruoyi.internet.api.enums.IntUserEnum;
import com.ruoyi.internet.domain.IntFoodMachinery;
import com.ruoyi.internet.mapper.IntFoodMachineryMapper;
import com.ruoyi.internet.mapper.IntUserMapper;
import com.ruoyi.internet.service.IIntFoodMachineryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 食品机械Service业务层处理
 *
 * @author cjie
 * @date 2021-08-16
 */
@Service
public class IntFoodMachineryServiceImpl implements IIntFoodMachineryService {
    @Autowired
    private IntFoodMachineryMapper intFoodMachineryMapper;
    @Autowired
    private IntUserMapper intUserMapper;

    /**
     * 查询食品机械
     *
     * @param id 食品机械ID
     * @return 食品机械
     */
    @Override
    public IntFoodMachinery selectIntFoodMachineryById(String id) {
        IntFoodMachinery intFoodMachinery = intFoodMachineryMapper.selectIntFoodMachineryById(id);
        IntUser intUser = intUserMapper.selectIntUserById(intFoodMachinery.getExpirationUserId());
        if(IntUserEnum.企业交易商.getCode().equals(intUser.getType())){
            intFoodMachinery.setExpirationName(intUser.getEnterpriseName());
        }
        intFoodMachinery.setType(intUser.getType());
        //未登录，去掉联系人和电话
        if (StringUtils.isEmpty(SecurityUtils.getUsername())) {
            intFoodMachinery.setLinkMan(null);
            intFoodMachinery.setPhone(null);
        }

        if(StringUtils.isNotEmpty(intFoodMachinery.getGoodsPic())){
            intFoodMachinery.setGoodsPicArray(JSON.parseArray(intFoodMachinery.getGoodsPic()));
        }

        return intFoodMachinery;
    }

    /**
     * 查询食品机械列表
     *
     * @param intFoodMachinery 食品机械
     * @return 食品机械
     */
    @Override
    public List<IntFoodMachinery> selectIntFoodMachineryList(IntFoodMachinery intFoodMachinery) {
        List<IntFoodMachinery> foodMachineryList = intFoodMachineryMapper.selectIntFoodMachineryList(intFoodMachinery);
        foodMachineryList.forEach(foodMachinery->{
            if(StringUtils.isNotEmpty(foodMachinery.getGoodsPic())){
                foodMachinery.setGoodsPicArray(JSON.parseArray(foodMachinery.getGoodsPic()));
            }
        });
        return foodMachineryList;

    }

    /**
     * 新增食品机械
     *
     * @param intFoodMachinery 食品机械
     * @return 结果
     */
    @Override
    public int insertIntFoodMachinery(IntFoodMachinery intFoodMachinery) {
        intFoodMachinery.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        intFoodMachinery.setCreateTime(DateUtils.getNowDate());
        intFoodMachinery.setCreateBy(SecurityUtils.getUsername());
        intFoodMachinery.setExpirationUserId(SecurityUtils.getUserId());

        if(intFoodMachinery.getGoodsPicArray() != null){
            intFoodMachinery.setGoodsPic(JSON.toJSONString(intFoodMachinery.getGoodsPicArray()));
        }
        return intFoodMachineryMapper.insertIntFoodMachinery(intFoodMachinery);
    }

    /**
     * 修改食品机械
     *
     * @param intFoodMachinery 食品机械
     * @return 结果
     */
    @Override
    public int updateIntFoodMachinery(IntFoodMachinery intFoodMachinery) {
        intFoodMachinery.setUpdateTime(DateUtils.getNowDate());
        intFoodMachinery.setUpdateBy(SecurityUtils.getUsername());
        if(intFoodMachinery.getGoodsPicArray() != null){
            intFoodMachinery.setGoodsPic(JSON.toJSONString(intFoodMachinery.getGoodsPicArray()));
        }
        return intFoodMachineryMapper.updateIntFoodMachinery(intFoodMachinery);
    }

    /**
     * 批量删除食品机械
     *
     * @param ids 需要删除的食品机械ID
     * @return 结果
     */
    @Override
    public int deleteIntFoodMachineryByIds(String[] ids) {
        return intFoodMachineryMapper.deleteIntFoodMachineryByIds(ids);
    }

    /**
     * 删除食品机械信息
     *
     * @param id 食品机械ID
     * @return 结果
     */
    @Override
    public int deleteIntFoodMachineryById(String id) {
        return intFoodMachineryMapper.deleteIntFoodMachineryById(id);
    }
}
